<?php

/**
 * Provides access to the Facebook Platform.
 *
 * @author Rashid Mohamad <rashid@contentcloudhq.com>
 */
class Facebook_Client {
	/**
	 * Version.
	 */
	const VERSION = '0.1.0';

	/**
	 * Default options for curl.
	 */
	public static $CURL_OPTS = array(
		CURLOPT_CONNECTTIMEOUT => 10,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_TIMEOUT				=> 60,
		CURLOPT_USERAGENT			=> 'facebook-php-2.0',
	);

	/**
	 * The Application ID.
	 */
	protected $appId;

	/**
	 * The Application API Secret.
	 */
	protected $apiSecret;

	/**
	 * The Application API Token.
	 */
	protected $userProfile;

	/**
	 * Initialize a Facebook Application.
	 *
	 * The configuration:
	 * - appId: the application ID
	 * - secret: the application secret
	 * - cookie: (optional) boolean true to enable cookie support
	 * - domain: (optional) domain for the cookie
	 *
	 * @param Array $config the application configuration
	 */
	public function __construct($config) {
		$this->setAppId( $config[ 'appId' ] );
		$this->setApiSecret( $config[ 'secret' ] );
		if( isset( $_GET[ 'code' ] ) ) {
			$token = $this->makeRequest( 'https://graph.facebook.com/oauth/access_token?client_id=' . $this->getAppId() . '&redirect_uri=' . urlencode( HOME_PATH . 'facebook-oauth-callback/' ) .  '&client_secret=' . $this->getApiSecret() . '&code=' . $_GET[ 'code' ] );
			$this->_token = $token;
			$this->_response = $response = $this->makeRequest( 'https://graph.facebook.com/me?' . $token );

			$this->userProfile = @json_decode( $response, true );
		}
	}

	public function getLoginUrl() {
		return 'https://www.facebook.com/dialog/oauth?client_id='. FACEBOOK_APP_ID . '&redirect_uri=' . urlencode( HOME_PATH . 'facebook-oauth-callback/' ) . '&scope=email,user_birthday,user_friends';
	}

	public function getUserProfile() {
		return $this->userProfile;
	}

	public function getUserFriends() {
		$userFriendLists = array();
		$userFriendListsArr = @json_decode( $this->makeRequest( 'https://graph.facebook.com/me/friends/?' . $this->_token ), true );
		if( count( $userFriendListsArr ) > 0 ){
			if( isset( $userFriendListsArr[ 'data' ] ) ){
				$userFriendLists = $userFriendListsArr[ 'data' ];
			}
		}
		return $userFriendLists;
	}

	/**
	 * Set the Application ID.
	 *
	 * @param String $appId the Application ID
	 */
	public function setAppId( $appId ) {
		$this->appId = $appId;
		return $this;
	}

	/**
	 * Get the Application ID.
	 *
	 * @return String the Application ID
	 */
	public function getAppId() {
		return $this->appId;
	}

	/**
	 * Set the API Secret.
	 *
	 * @param String $appId the API Secret
	 */
	public function setApiSecret( $apiSecret ) {
		$this->apiSecret = $apiSecret;
		return $this;
	}

	/**
	 * Get the API Secret.
	 *
	 * @return String the API Secret
	 */
	public function getApiSecret() {
		return $this->apiSecret;
	}

	/**
	 * Makes an HTTP request. This method can be overriden by subclasses if
	 * developers want to do fancier things or use something other than curl to
	 * make the request.
	 *
	 * @param String $url the URL to make the request to
	 * @param Array $params the parameters to use for the POST body
	 * @param CurlHandler $ch optional initialized curl handle
	 * @return String the response text
	 */
	protected function makeRequest( $url, $ch = null ) {
		if( !$ch ) {
			$ch = curl_init();
		}
		$opts = self::$CURL_OPTS;
		$opts[ CURLOPT_SSL_VERIFYPEER ] = false;
		$opts[ CURLOPT_URL ] = $url;
		curl_setopt_array( $ch, $opts );
		$result = curl_exec( $ch );
		curl_close( $ch );
		return $result;
	}
}